#ifndef GAME_ANIMATION_GRASS2_SHADER_H
#define GAME_ANIMATION_GRASS2_SHADER_H
//
// Game Animation.
// Copyright (C) 2007 Department of Computer Science, University of Copenhagen
//http://www.cs.ubc.ca/~heidrich/Papers/EGShort.02.pdf
#include <animation/configuration.h>

#include <OpenTissue/gpu/cg/cg_program.h>
#include <OpenTissue/gpu/image/image.h>
#include <OpenTissue/gpu/image/io/image_read.h>
#include <OpenTissue/gpu/image/io/image_write.h>
#include <OpenTissue/gpu/image/image_make_noise.h>
#include <OpenTissue/gpu/texture/texture_texture2D.h>
#include <OpenTissue/gpu/texture/texture_create_texture2D.h>
#include <math.h>


namespace animation
{

  /**
  * The Terrain Shader.
  * Sets up rendering before drawing terrain geometry.
  */
  class Grass2Shader
  {
  public:

    typedef boost::shared_ptr<OpenTissue::image::Image<unsigned char> > image_pointer;

  protected:

    OpenTissue::cg::Program       m_vertex_program;
    OpenTissue::cg::Program       m_fragment_program;
    image_pointer                 m_grass_image;
    OpenTissue::texture::texture2D_pointer m_grass_texture;
  public:

    void pre_render(bool reload,float light_x,float light_y,float light_z,
		                        float intensity,
								float fog_density,
	                            float fog_color_r, float fog_color_g, float fog_color_b,float alpha,float h,float wx,float wy)
    {
      //--- Load textures if they are not allready loaded
      if(!m_grass_image)
      {
        m_grass_image.reset( new OpenTissue::image::Image<unsigned char>() );
		OpenTissue::image::read(demo_path + "TheSwampThing2008/data/grass.png",*m_grass_image);
        m_grass_texture = m_grass_image->create_texture(GL_RGBA);
        m_grass_texture->set_repeating(true);
       // m_grass_texture->set_mipmapping( true );
      }

      //--- Load vertex program, note that cg-program path is currently hardcoded, a
      //--- more sensible solution would be
      //---
      //---  std::string filename = get_environment_variable("OPENTISSUE") + .... + "vp_grass.cg";
      //---
      if(!m_vertex_program.is_program_loaded()||reload)
        m_vertex_program.load_from_file(OpenTissue::cg::Program::vertex_program,demo_path + "../animation/shader/grass/grass2_vp.cg");

      m_vertex_program.set_modelview_projection_matrix();
	  m_vertex_program.set_modelview_inverse_transpose();
	  m_vertex_program.set_float_param("light_vec",light_x,light_y,light_z);
	  m_vertex_program.set_float_param("intensity",intensity);
	  m_vertex_program.set_float_param("h",h);
	  m_vertex_program.set_float_param("wind",wx,wy);
      //--- load fragment program
      if(!m_fragment_program.is_program_loaded()||reload)
        m_fragment_program.load_from_file(OpenTissue::cg::Program::fragment_program,demo_path + "../animation/shader/grass/grass2_fp.cg");

	  m_fragment_program.set_float_param("alpha",alpha);
      m_fragment_program.set_input_texture( "grass_texture",  m_grass_texture  );

      //--- Setup openGL state
      //glEnable(GL_CULL_FACE);
    }

    void enable()
    {
      m_vertex_program.enable();
      m_fragment_program.enable();
	  //glEnable(GL_CULL_FACE);
    }
    void disable()
    {
      m_vertex_program.disable();
      m_fragment_program.disable();
	 // glDisable(GL_CULL_FACE);
    }
  };

} // namespace animation

// GAME_ANIMATION_TERRAIN_SHADER_H
#endif
